<!--
    $(document).ready
	(
		function () 
		{
			var validateArtistname = $('#validateArtistname');
			$('#artistname').blur
			(
				function () 
				{
					// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
					var t = this;
					
					// only run the check if the username has actually changed - also means we skip meta keys
					if ((this.value != this.lastValue))
					{
						
						// the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
						// a particular key, it will only fire when the release the key.
																
						// show our holding text in the validation message space
						validateArtistname.removeClass('error').html('<img src="/img/ajax-loader.gif" height="16" width="16" />');
						
						if(t.value.length > 0)
						{	
                                                    $.ajax({
                                                            url: '/inc/validate_artist.inc.php',
                                                            data: 'action=check_artistname&artistname=' + t.value,
                                                            dataType: 'json',
                                                            type: 'post',
                                                            success: function (j) {
                                                                    if(j.ok)
                                                                            validateArtistname.html('<font color="green"><b>'+j.msg+'</b></font>');
                                                                    else
                                                                            validateArtistname.html('<font color="red">'+j.msg+'</font>');
                                                                }
                                                            });
 
                                                }
                                                else
                                                {
                                                    validateArtistname.html('<font color="red">Wrong characters</color>');
                                                }
							
                                                 if(this.value.length == 0)
                                                    validateArtistname.html('');
					}
					
                                        if(this.value.length == 0)
						validateArtistname.html('');
				}
					
			);
											
		}
	);
    //-->

var member_count = 0;
var guitarist_count = 0;
var bassist_count = 0;
var drummer_count = 0;
var vocalist_count = 0;
var tag_count = 0;
var label_count = 0;
var release_count = 0;

function getMemberValues()
{
	var div = document.getElementById('members');
	var inputs = div.getElementsByTagName("input");	
	var i = 0;	
	var a = new Array();
	
	for(i = 0; i < inputs.length; i++)
	{
		a[i] = inputs[i].value;
	}
	
	return a;
}

function getTagValues()
{
	var div = document.getElementById('tags');
	var inputs = div.getElementsByTagName("input");
	var i = 0;
	var a = new Array();
	
	for(i = 0; i < inputs.length; i++)
	{
		a[i] = inputs[i].value;
	}
	
	return a;
}

function getLabelValues()
{
	var div = document.getElementById('labels');
	var inputs = div.getElementsByTagName("input");
	var i = 0;
	var a = new Array();

	for(i = 0; i < inputs.length; i++)
	{
		a[i] = inputs[i].value;
	}

	return a;
}

function getReleaseValues()
{
	var div = document.getElementById('releases');
	var inputs = div.getElementsByTagName("input");
	var i = 0;
	var a = new Array();

	for(i = 0; i < inputs.length; i++)
	{
		a[i] = inputs[i].value;
	}

	return a;
}

function setMemberValues(arr)
{
	var div = document.getElementById('members');
	var inputs = div.getElementsByTagName("input");
	
	for(var i in arr)
	{
		inputs[i].value = arr[i];
	}
}

function setTagValues(arr)
{
	var div = document.getElementById('tags');
	var inputs = div.getElementsByTagName("input");
	
	for(var i in arr)
	{
		inputs[i].value = arr[i];
	}
}

function setLabelValues(arr)
{
	var div = document.getElementById('labels');
	var inputs = div.getElementsByTagName("input");

	for(var i in arr)
	{
		inputs[i].value = arr[i];
	}
}

function setReleaseValues(arr)
{
	var div = document.getElementById('releases');
	var inputs = div.getElementsByTagName("input");

	for(var i in arr)
	{
		inputs[i].value = arr[i];
	}
}

function addTag()
{
	var div = document.getElementById('tags');
	
	var arr = getTagValues();
	div.innerHTML += '<span id="tag'+tag_count+'"><input id="input_tag'+tag_count+'" type="text" class="flat-tag" size="30" maxlength="64" name="tag'+tag_count+'">&nbsp;<small><a href="#" class="button" onclick="removeTag('+tag_count+'); return false;">remove</a></small><br><br></span>';
	setTagValues(arr);
	
	tag_count++;
}

function removeTag(id)
{
	var o = document.getElementById("tag"+id);
	o.style.display='none';
	var i = document.getElementById('input_tag'+id);
	i.value = '';
}

function addLabel()
{
	var div = document.getElementById('labels');

	var arr = getLabelValues();
	div.innerHTML += '<span id="label'+label_count+'"><input id="input_label_year'+label_count+'" type="text" class="flat" size="2" name="label_year'+label_count+'">&nbsp;<input id="input_label'+label_count+'" type="text" class="flat" size="22" maxlength="128" name="label'+label_count+'">&nbsp;<small><a href="#" class="button" onclick="removeLabel('+label_count+');">remove</a></small><br><br></span>';
	setLabelValues(arr);

	label_count++;
}

function removeLabel(id)
{
	var o = document.getElementById("label"+id);
	o.style.display='none';
	var i = document.getElementById('input_label'+id);
	i.value = '';
}

function addRelease()
{
	var div = document.getElementById('releases');

	var arr = getReleaseValues();
	div.innerHTML += '<span id="release'+release_count+'"><input id="input_release_year'+release_count+'" type="text" class="flat" size="2" name="release_year'+release_count+'">&nbsp<input id="input_release'+release_count+'" type="text" class="flat" size="22" maxlength="128" name="release'+release_count+'">&nbsp;<small><a href="#" class="button" onclick="removeRelease('+release_count+'); return false;">remove</a></small><br><br></span>';
	setReleaseValues(arr);

	release_count++;
}

function removeRelease(id)
{
	var o = document.getElementById("release"+id);
	o.style.display='none';
	var i = document.getElementById('input_release'+id);
	i.value = '';
}

function addGuitarist()
{
	var div = document.getElementById('members');
	
	var arr = getMemberValues();
	div.innerHTML += '<span id="member_guitarist'+guitarist_count+'" ><input id="input_member_year_started'+member_count+'" type="text" class="flat" size="2" name="member_year_started'+member_count+'">&nbsp;<input id="input_member_year_finished'+member_count+'" type="text" class="flat" size="2" name="member_year_finished'+member_count+'">&nbsp;<input id="input_member_guitarist'+guitarist_count+'" type="text" class="flat-guitar" size="20" maxlength="128" name="member_guitarist'+guitarist_count+'">&nbsp;<small><a href="#" class="button" onclick="removeGuitarist('+guitarist_count+'); return false;">remove</a></small><br><br></span>';
	setMemberValues(arr);
	
	guitarist_count++;
	member_count++;
}

function removeGuitarist(id)
{
	var o = document.getElementById("member_guitarist"+id);
	o.style.display='none';
	var i = document.getElementById('input_member_guitarist'+id);
	i.value = '';
}

function addBassist()
{
	var div = document.getElementById('members');
	
	var arr = getMemberValues();
	div.innerHTML += '<span id="member_bassist'+bassist_count+'" ><input id="input_member_year_started'+member_count+'" type="text" class="flat" size="2" name="member_year_started'+member_count+'">&nbsp;<input id="input_member_year_finished'+member_count+'" type="text" class="flat" size="2" name="member_year_finished'+member_count+'">&nbsp;<input id="input_member_bassist'+bassist_count+'" type="text" class="flat-bass" size="20" maxlength="128" name="member_bassist'+bassist_count+'">&nbsp;<small><a href="#" class="button" onclick="removeBassist('+bassist_count+'); return false;">remove</a></small><br><br></span>';
	setMemberValues(arr);
	
	bassist_count++;
	member_count++;
}

function removeBassist(id)
{
	var o = document.getElementById("member_bassist"+id);
	o.style.display='none';
	var i = document.getElementById('input_member_bassist'+id);
	i.value = '';
}

function addDrummer()
{
	var div = document.getElementById('members');
	
	var arr = getMemberValues();
	div.innerHTML += '<span id="member_drummer'+drummer_count+'" ><input id="input_member_year_started'+member_count+'" type="text" class="flat" size="2" name="member_year_started'+member_count+'">&nbsp;<input id="input_member_year_finished'+member_count+'" type="text" class="flat" size="2" name="member_year_finished'+member_count+'">&nbsp;<input id="input_member_drummer'+drummer_count+'" type="text" class="flat-drums" size="20" maxlength="128" name="member_drummer'+drummer_count+'">&nbsp;<small><a href="#" class="button" onclick="removeDrummer('+drummer_count+'); return false;">remove</a></small><br><br></span>';
	setMemberValues(arr);
	
	drummer_count++;
	member_count++;
}

function removeDrummer(id)
{
	var o = document.getElementById("member_drummer"+id);
	o.style.display='none';
	var i = document.getElementById('input_member_drummer'+id);
	i.value = '';
}

function addVocalist()
{
	var div = document.getElementById('members');
	
	var arr = getMemberValues();
	div.innerHTML += '<span id="member_vocalist'+vocalist_count+'" ><input id="input_member_year_started'+member_count+'" type="text" class="flat" size="2" name="member_year_started'+member_count+'">&nbsp;<input id="input_member_year_finished'+member_count+'" type="text" class="flat" size="2" name="member_year_finished'+member_count+'">&nbsp;<input id="input_member_vocalist'+vocalist_count+'" type="text" class="flat-vocals" size="20" maxlength="128" name="member_vocalist'+vocalist_count+'">&nbsp;<small><a href="#" class="button" onclick="removeVocalist('+vocalist_count+'); return false;">remove</a></small><br><br></span>';
	setMemberValues(arr);
	
	vocalist_count++;
	member_count++;
}

function removeVocalist(id)
{
	var o = document.getElementById("member_vocalist"+id);
	o.style.display='none';
	var i = document.getElementById('input_vocalist_drummer'+id);
	i.value='';
}


function checkForm()
{
    var i = document.getElementById("artistname");
    var err = $('#error');
    err.html('');

    if(i.value.length == 0)
    {
            err.html('<font color="red">Please specify artist name</font>');
            return false;
    }
    
    i = document.getElementById("picture");
    
    if(i.value.length > 0)
    {
	if(i.value.toLowerCase.indexOf(".jpg") != i.value.length - 4)
	{
	    err.html('<font color="red">Only JPEG images are allowed</font>');
	    return false;
	}
    }


    /*

    i = document.getElementById("year_formed");

    if(i.value.length == 0)
    {
            err.html('<font color="red">Not all fields are filled</font>');
            return false;
    }

    i = document.getElementById("homepage");

    if(i.value.length == 0)
    {
            err.html('<font color="red">Not all fields are filled</font>');
            return false;
    }
    */
   /*
    var div = document.getElementById("members");

    var inputs = div.getElementsByTagName("input");

    i = 0;

    for(i = 0; i < inputs.length; i++)
    {
        if(inputs[i].value.length == 0)
        {
                err.html('<font color="red">Not all fields are filled</font>');
                return false;
        }
    }
    */

    div = document.getElementById("tags");

    inputs = div.getElementsByTagName("input");

    i = 0;

    for(i = 0; i < inputs.length; i++)
    {
        if(inputs[i].value.length == 0)
        {
                err.html('<font color="red">Not all fields are filled</font>');
                return false;
        }
    }

    return true;
}

var opts_state = 1;
var opts_count = 9;

function switchOptions()
{
    if(opts_state == 0)
    {
	var i = 0;
	for(i = 1; i <= opts_count; i++)
	{
	    var opts = document.getElementById('opts'+i);
	    opts.style.display = 'none';
	}

	var link = document.getElementById('switch_opts');
	link.innerHTML = 'More options';
	opts_state = 1;
    }
    else
    {
	var i = 0;
	for(i = 1; i <= opts_count; i++)
	{
	    var opts = document.getElementById('opts'+i);
	    opts.style.display = 'table-row';
	}

	var link = document.getElementById('switch_opts');
	link.innerHTML = 'Less options';
	opts_state = 0;
    }
}


